// Parse LRC text
function handleLRC(content) {
  let baseInfo = {};
  let timeInfo = [];
  for (let i = 0;i < content.length;i++) {
    let line = content[i];
    if (line.indexOf("[") === -1 || line.indexOf("]") === -1) {
      continue;
    }
    let _content = line.substring(line.lastIndexOf("]") + 1);
    for (;;) {
      if (line.indexOf("[") === -1 || line.indexOf("]") === -1) {
        break;
      }
      let tag = line.substring(line.indexOf("[") + 1, line.indexOf("]"));
      let tag_split = tag.split(":");
      line = line.substring(line.indexOf("]") + 1);
      if (tag_split.length === 2) {
        if (tag_split[1].indexOf(".") === -1 || isNaN(parseInt(tag_split[0]))) {
          // [xx:xx]
          baseInfo[tag_split[0]] = tag_split[1];
        } else {
          // [minute:second.millisecond]
          let minute = parseInt(tag_split[0]);
          let second = parseInt(tag_split[1].split(".")[0]);
          let millisecond = parseInt(tag_split[1].split(".")[0]);
          timeInfo.push({
            time: {
              baseForm: tag,
              minute, second, millisecond,
              htmlCurrentTime: (minute * 60) + second + (millisecond / 1000)
            },
            content: _content
          });
        }
        continue;
      }
      // [minute:second:millisecond] or [hour:minute:second.millisecond]
      if (tag_split.length === 3) {
        if (tag_split[2].indexOf(".") === -1) {
          // [minute:second:millisecond]
          let minute = parseInt(tag_split[0]);
          let second = parseInt(tag_split[1]);
          let millisecond = parseInt(tag_split[2]);
          timeInfo.push({
            time: {
              baseForm: tag,
              minute, second, millisecond,
              htmlCurrentTime: (minute * 60) + second + (millisecond / 1000)
            },
            content: _content
          });
        } else {
          // [hour:minute:second.millisecond]
          let hour = parseInt(tag_split[0]);
          let minute = parseInt(tag_split[1]);
          let second = parseInt(tag_split[2].split(".")[0]);
          let millisecond = parseInt(tag_split[2].split(".")[1]);
          timeInfo.push({
            time: {
              baseForm: tag,
              hour, minute, second, millisecond,
              htmlCurrentTime: (hour * 3600) + (minute * 60) + second + (millisecond / 1000)
            },
            content: _content
          });
        }
        continue;
      }
      // [hour:minute:second:millisecond]
      if (tag_split.length === 4) {
        let hour = parseInt(tag_split[0]);
          let minute = parseInt(tag_split[1]);
          let second = parseInt(tag_split[2]);
          let millisecond = parseInt(tag_split[3]);
          timeInfo.push({
            time: {
              baseForm: tag,
              hour, minute, second, millisecond,
              htmlCurrentTime: (hour * 3600) + (minute * 60) + second + (millisecond / 1000)
            },
            content: _content
          });
        continue;
      }
    }
  }
  return mergeResult(baseInfo, timeInfo);
}

function mergeResult(baseInfo, timeInfo) {
  let _result = {content: []};
  for (let key in baseInfo) {
    if (key === "offset") {
      _result[key] = parseInt(baseInfo[key]) || 0;
    } else {
      _result[key] = baseInfo[key];
    }
  }
  for (let i = 0;i < timeInfo.length;i++) {
    _result.content.push({
      time: timeInfo[i].time,
      content: timeInfo[i].content
    });
  }
  return _result;
}

module.exports = handleLRC;